package com.emcom.gotour.mvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.emcom.gotour.enums.PagingAction;
import com.emcom.gotour.mvc.model.TourContentModel;
import com.emcom.gotour.po.TourContent;
import com.emcom.gotour.service.FileUploadService;
import com.emcom.gotour.service.TourCategoryService;
import com.emcom.gotour.service.TourContentService;
import com.emcom.gotour.service.TourManageService;
import com.emcom.gotour.utils.Paging;
import com.emcom.gotour.utils.PagingUtils;
import com.emcom.gotour.validation.MultipartFileValidator;
import com.emcom.gotour.vo.TourContentVO;

@Controller
public class UpdateTourController {

	@Autowired
	private TourContentService tourContentService;

	@Autowired
	private TourCategoryService tourCategoryService;

	@Autowired
	private TourManageService tourManageService;

	@Autowired
	private MessageSource messageSource;

	@Autowired
	private FileUploadService fileUploadService;

	@Autowired
	private MultipartFileValidator multipartFileValidator;

	@RequestMapping(value={"/admin/update_tour.html"})
	public String updateTour(HttpServletRequest request) {
		TourContentVO tourContentVO = tourContentService.queryContent(Long.valueOf(request.getParameter("id")));
		TourContentModel.getUpdateView(request, messageSource, tourCategoryService, tourContentVO);
		TourContentModel.setObject(request, tourContentVO);
		return "/admin/update_tour";
	}

	@RequestMapping(value={"/admin/update_tour_action.html"})
	public String updateTourAction(
			@RequestParam(value="adImageFile", required=false) MultipartFile adImageFile,
			@RequestParam(value="imageFile", required=false) MultipartFile imageFile,
			@RequestParam(value="imageFile1", required=false) MultipartFile imageFile1,
			@RequestParam(value="imageFile2", required=false) MultipartFile imageFile2,
			@RequestParam(value="imageFile3", required=false) MultipartFile imageFile3,
			@RequestParam(value="imageFile4", required=false) MultipartFile imageFile4,
			HttpServletRequest request) {
		try {
			if(adImageFile != null) {
				multipartFileValidator.validate(adImageFile);
			}
			if(imageFile != null) {
				multipartFileValidator.validate(imageFile);
			}
			if(imageFile1 != null) {
				multipartFileValidator.validate(imageFile1);
			}
			if(imageFile2 != null) {
				multipartFileValidator.validate(imageFile2);
			}
			if(imageFile3 != null) {
				multipartFileValidator.validate(imageFile3);
			}
			if(imageFile4 != null) {
				multipartFileValidator.validate(imageFile4);
			}
			TourContent tourContent = TourContentModel.getObject(request, fileUploadService, adImageFile, imageFile, imageFile1, imageFile2, imageFile3, imageFile4);
			tourManageService.updateTour(tourContent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Paging paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		if(paging == null) {
			PagingUtils.forceInitPaging(request, tourManageService.queryMultiContent(null, null, null), PagingUtils.PAGE_SIZE, TourContentModel.PAGING_NAME);
			paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		}
		String page = String.valueOf(paging.getCurrentPage());
		String action = PagingAction.TO.name();
		PagingUtils.forceInitPaging(request, tourManageService.queryMultiContent(null, null, null), paging.getPageSize(), TourContentModel.PAGING_NAME);
		paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		paging = PagingUtils.paging(PagingAction.valueOf(action), paging, Integer.valueOf(page));
		PagingUtils.savePaging(request, paging, TourContentModel.PAGING_NAME);
		List<TourContentVO> tourContentVOList = TourContentModel.buildTourContentVOList(paging.getPagingList());
		TourContentModel.getView(request, messageSource, tourCategoryService, tourContentVOList);
		request.setAttribute("page", paging.getCurrentPage());
		request.setAttribute("total", paging.getLastPage());
		request.setAttribute("pageSize", paging.getPageSize());
		return "/admin/tour";
	}

}
